<?php
namespace app\index\controller;

use app\common\controller\Front;

class Article extends Front
{

    protected $_article_limit = 10; // 分类下文章展示数量
    protected $_new_limit = 5; // 左边新文章展示数量
                               // 文章列表
    public function index()
    {
        $id = $this->request->param('cate_id/d');
        // 文章内容
        $article_all = $this->db_lists_page('article', [
            'type_id' => $id,
            'status' =>1,
            'isdelete'=>0
        ], $this->_article_limit, [
            'order' => 'id desc'
        ]);
        // 文章分类列表（商家帮助指南分类不显示）
        $type_list = \Fields::getById('article');
        $guide_info = 0; // 商家帮助指南信息
        if (isset($type_list) && ! empty($type_list)) {
            foreach ($type_list as $key => $val) {
                if ($val['tag'] == 'seller_guide') {
                    $guide_info = $val;
                    unset($type_list[$key]);
                }
            }
        }
        // 本文的分类名称
        $type_name = $type_list[$id]['name'];
        
        // 本分类的最新文章,商家帮助指南下的文章不显示
        $no_type = ''; // 网站网站也不显示
        if (isset($guide_info['_child'])) {
            $no_type .= implode(',', array_keys(reset_by_key($guide_info['_child'], 'id')));
        }
        
        $new_list = rpc('common/common/lists', [
            'article',
            [
                'type_id' => $id,
                'isdelete' => 0,
                'status' => 1
            ],
            [
                'page' => '1,' . $this->_new_limit,
                'order' => 'id desc'
            ]
        ]);
        $this->view->assign('type_id', $id);
        $this->view->assign('article_all', $article_all);
        $this->view->assign('type_name', $type_name);
        $this->view->assign('type_list', $type_list);
        $this->view->assign('new_list', $new_list);
        $this->assign('_page', [
            'title' => '爱员工官网',
            'name' => $type_name
        ]);
        return $this->view->fetch();
    }
    
    // 文章详情
    public function detail()
    {
        $id = $this->request->param('article_id/d');
        // 文章内容
        $article = rpc('common/common/one', [
            'article',
            [
                'id' => $id,
                'isdelete' => 0,
                'status' => 1
            ]
        ]);
        // 文章分类列表（商家帮助指南分类不显示）
        $type_list = \Fields::getById('article');
        
        // 本文的分类名称
        $type_name = $type_list[$article['type_id']]['name'];
        
        $guide_info = 0; // 商家帮助指南信息
        if (isset($type_list) && ! empty($type_list)) {
            foreach ($type_list as $key => $val) {
                if ($val['tag'] == 'seller_guide') {
                    $guide_info = $val;
                    unset($type_list[$key]);
                }
            }
        }
        
        // to do 文章详情上一篇 下一篇
        
        // 本分类的最新文章,商家帮助指南下的文章不显示
        $no_type = ''; // 网站网站也不显示
        if (isset($guide_info['_child'])) {
            $no_type .= implode(',', array_keys(reset_by_key($guide_info['_child'], 'id')));
        }
        $new_list = rpc('common/common/lists', [
            'article',
            [
                'type_id' => $article['type_id'],
                'isdelete' => 0,
                'status' => 1
            ],
            [
                'page' => '1,' . $this->_new_limit,
                'order' => 'id desc'
            ]
        ]);
        $this->view->assign('article_id', $id);
        $this->view->assign('article', $article);
        $this->view->assign('type_name', $type_name);
        $this->view->assign('type_list', $type_list);
        $this->view->assign('new_list', $new_list);
        $this->assign('_page', [
            'title' => '爱员工官网',
            'name' => $article['title']
        ]);
        return $this->view->fetch();
    }
}
